package cn.tedu.csmallproducts.controller;

import cn.tedu.csmallproducts.pojo.dto.AlbumAddNewDTO;
import cn.tedu.csmallproducts.pojo.dto.BrandAddNewDTO;
import cn.tedu.csmallproducts.pojo.dto.BrandUpdateDTO;
import cn.tedu.csmallproducts.pojo.vo.AlbumListItemVO;
import cn.tedu.csmallproducts.pojo.vo.AlbumStandardVO;
import cn.tedu.csmallproducts.pojo.vo.BrandListItemVO;
import cn.tedu.csmallproducts.pojo.vo.BrandStandardVO;
import cn.tedu.csmallproducts.service.IBrandService;
import cn.tedu.csmallproducts.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/brand")
@Api(tags = "04.品牌管理模块")
@Validated
public class BrandController {
    @Autowired
    private IBrandService iBrandService;

    @PostMapping("/add-new")
    @ApiOperation("添加品牌")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid BrandAddNewDTO brandAddNewDTO){
        log.debug("开始处理【添加品牌】的请求，参数：{}", brandAddNewDTO);
            iBrandService.addNew(brandAddNewDTO);
            return JsonResult.ok();

    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询品牌详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<BrandStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【根据ID查询品牌详情】的请求，参数：{}", id);
        BrandStandardVO queryResult = iBrandService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("")
    @ApiOperation("查询品牌列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<BrandListItemVO>> list(){
        log.debug("开始处理【查询品牌列表数据】的业务，无参数");
        List<BrandListItemVO> list = iBrandService.list();
        return JsonResult.ok(list);

    }
    // http://localhost:9080/brands/9527/update
    @ApiOperation("修改品牌详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "品牌id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, BrandUpdateDTO brandUpdateDTO) {
        log.debug("开始处理【修改品牌详情】的请求，参数ID：{}, 新数据：{}", id, brandUpdateDTO);
        iBrandService.updateInfoById(id, brandUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/brands/9527/enable
    @ApiOperation("启用品牌")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParam(name = "id", value = "品牌id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("开始处理【启用品牌】的请求，参数：{}", id);
        iBrandService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/brands/9527/disable
    @ApiOperation("禁用品牌")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParam(name = "id", value = "品牌id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("开始处理【禁用品牌】的请求，参数：{}", id);
        iBrandService.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/brands/9527/delete
    @ApiOperation("根据id删除品牌")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "品牌id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id) {
        log.debug("开始处理【根据id删除品牌】的请求，参数：{}", id);
        iBrandService.delete(id);
        return JsonResult.ok();
    }

}
